﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class ModelMatHangDA
    {
        public ModelMatHangDA()
        {

        }

        /// <summary>
        /// Hàm thêm 1 moodel mặt hàng mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(ModelMatHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaLoaiMH", entity.MaLoaiMH);
                pb.AddParameter("Model", entity.Model);
                pb.AddParameter("HangSX", entity.HangSX);
                pb.AddParameter("NuocSX", entity.NuocSX);
                pb.AddParameter("CauHinh", entity.CauHinh);
                pb.AddParameter("BaoHanh", entity.BaoHanh);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("MaDonViTinh", entity.MaDonViTinh);
                return (int)DBFactory.Database.ExecuteNonQuery("ModelMatHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm Thêm 1 model mới, trả về ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertGetID(ModelMatHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaLoaiMH", entity.MaLoaiMH);
                pb.AddParameter("Model", entity.Model);
                pb.AddParameter("HangSX", entity.HangSX);
                pb.AddParameter("NuocSX", entity.NuocSX);
                pb.AddParameter("CauHinh", entity.CauHinh);
                pb.AddParameter("BaoHanh", entity.BaoHanh);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("MaDonViTinh", entity.MaDonViTinh);
                return (int)DBFactory.Database.ExecuteNonQueryGetID("ModelMatHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm thêm 1 model mặt hàng, dùng transaction
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int InsertTS(ModelMatHangEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaLoaiMH", entity.MaLoaiMH);
                pb.AddParameter("Model", entity.Model);
                pb.AddParameter("HangSX", entity.HangSX);
                pb.AddParameter("NuocSX", entity.NuocSX);
                pb.AddParameter("CauHinh", entity.CauHinh);
                pb.AddParameter("BaoHanh", entity.BaoHanh);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("MaDonViTinh", entity.MaDonViTinh);

                return (int)DBFactory.Database.ExecuteNonQueryGetID("ModelMatHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm cập nhật thông tin model mặt hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(ModelMatHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("MaLoaiMH", entity.MaLoaiMH);
                pb.AddParameter("Model", entity.Model);
                pb.AddParameter("HangSX", entity.HangSX);
                pb.AddParameter("NuocSX", entity.NuocSX);
                pb.AddParameter("CauHinh", entity.CauHinh);
                pb.AddParameter("BaoHanh", entity.BaoHanh);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("TinhTrang", entity.TinhTrang);
                pb.AddParameter("MaDonViTinh", entity.MaDonViTinh);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("ModelMatHang_UpDate", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm cập nhật trạng thái model mặt hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update_TinhTrang(ModelMatHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("TinhTrang", entity.TinhTrang);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("ModelMatHang_UpDate_TinhTrang", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm tất cả thông tin của model mặt hàng
        /// </summary>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_All(bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("ModelMatHang_FindAll", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        //public DataTable Find_ID(int MaModelMH, bool View)
        //{
        //    try
        //    {
        //        DataTable dt = new DataTable();
        //        ParameterBuilder pd = DBFactory.CreateParamBuilder();
        //        pd.AddParameter("MaModelMH", MaModelMH);
        //        pd.AddParameter("View", View);

        //        dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_ID", pd.Parameters);
        //        return dt;
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Write(ex);
        //        throw new Exception(ex.Message, ex);
        //    }
        //}

        /// <summary>
        /// Hàm tìm model mặt hàng theo Hãng sản xuất
        /// </summary>
        /// <param name="hang"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_HangSX(string hang, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("HangSX", hang);
                pd.AddParameter("View", View);
                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_HangSX", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng theo tên model
        /// </summary>
        /// <param name="model"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_Model(string model, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("Model", model);
                pd.AddParameter("View", View);
                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_Model", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng theo id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_ID(int id, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaModelMH", id);
                pd.AddParameter("View", View);
                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_ID", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng mã loại mặt hàng
        /// </summary>
        /// <param name="maloai"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_MaLoaiMH(int maloai, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaLoaiMH", maloai);
                pd.AddParameter("View", View);
                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_MaLoaiMH", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng theo tên loại mặt hàng
        /// </summary>
        /// <param name="loai"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_TenLoaiMH(string loai, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("TenLoaiMH", loai);
                pd.AddParameter("View", View);
                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_TenLoaiMH", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng Hãng sản xuất và model
        /// </summary>
        /// <param name="hang"></param>
        /// <param name="model"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_Hang_Model(string hang, string model, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("HangSX", hang);
                pd.AddParameter("Model", model);
                pd.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_Hang_Model", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm model mặt hàng theo mã hóa đơn bán
        /// </summary>
        /// <param name="MaHoaDonBan"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_MaHoaDonBan(int MaHoaDonBan, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaHoaDonBan", MaHoaDonBan);
                pd.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_MaHoaDonBan", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }
        /// <summary>
        /// Hàm tìm model mặt hàng theo mã hóa đơn nhập
        /// </summary>
        /// <param name="MaHoaDonNhap"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_MaHoaDonNhap(int MaHoaDonNhap, bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaHoaDonNhap", MaHoaDonNhap);
                pd.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_MaHoaDonNhap", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable Find_Info_Ban()
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();

                dt = DBFactory.Database.FillDataTable("ModelMatHang_Find_Info_Ban", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm ID model mặt hàng
        /// </summary>
        /// <param name="hang"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public int GetID(string hang, string model)
        {
            try
            {
                int kq = 0;
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("HangSX", hang);
                pd.AddParameter("Model", model);

                kq = (int)DBFactory.Database.ExecuteScalar("ModelMatHang_GetID", pd.Parameters);
                return kq;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm lấy giá của model mặt hàng
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <returns></returns>
        public decimal GetGia(int MaModelMH)
        {
            try
            {
                decimal kq = 0;
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaModelMH", MaModelMH);

                kq = (decimal)DBFactory.Database.ExecuteScalar("ModelMatHang_GetGia", pd.Parameters);
                return kq;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm xóa model mặt hàng
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            try
            {
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaModelMH", id);

                return DBFactory.Database.ExecuteNonQuery("ModelMatHang_Delete", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm chuyễn 1 Datable thành 1 List ModelMatHangEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<ModelMatHangEntity> ConvertToList(DataTable dt)
        {
            List<ModelMatHangEntity> list = new List<ModelMatHangEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int id = (int)dt.Rows[i][0];
                int maloai = (int)dt.Rows[i][1];
                string model = dt.Rows[i][2].ToString();
                string hangsx = dt.Rows[i][3].ToString();
                string nuocsx = dt.Rows[i][4].ToString();
                string cauhinh = dt.Rows[i][5].ToString();
                int baohanh = (int)dt.Rows[i][6];
                int soluong = (int)dt.Rows[i][7];
                int tinhtrang = (int)dt.Rows[i][8];
                int dvt = (int)dt.Rows[i][9];

                ModelMatHangEntity entity = new ModelMatHangEntity(id, model, hangsx, maloai, nuocsx,
                    cauhinh, baohanh, soluong, tinhtrang, dvt);
                list.Add(entity);
            }
            return list;
        }
    }
}
